package org.example;

import org.w3c.dom.ls.LSInput;

import java.util.Objects;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

class test {

    static int flag = 0;
    static Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(() -> {
            int i = 0;

            for(i = 0 ;i < 10 ;i=i+2)
            {
                synchronized (lock) {
                    while (flag != 0) {
                       try {
                           lock.wait();
                       }catch (Exception e)
                       {
                           e.printStackTrace();
                       }
                    }
                System.out.println(Thread.currentThread().getName() + "1");
                flag = 1;
                lock.notifyAll();
            }

            }
        });

        Thread t2 = new Thread(() -> {
            int i = 0;
            synchronized (lock) {
            for(i = 1 ;i < 10 ;i=i+2)
            {

                    while (flag != 1) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }

                System.out.println(Thread.currentThread().getName() + "2");
                flag = 2;
                lock.notifyAll();
            }
            }
        });


        Thread t3 = new Thread(() -> {
            int i = 0;
            synchronized (lock) {
                for(i = 1 ;i < 10 ;i=i+2)
                {

                    while (flag != 2) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }

                    System.out.println(Thread.currentThread().getName() + "3");
                    flag = 0;
                    lock.notifyAll();
                }
            }
        });

        t1.start();
        t2.start();
        t3.start();

    }

}


